/*
 *   Copyright (C) 2009  Jens Kieselbach
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */

package de.kieselbach.swing

import javax.swing.*
import java.awt.*

class UIUtilities {

    static void centerDialog(Window frame, def dialog) {

        Toolkit toolkit = Toolkit.getDefaultToolkit()
        def screensize = toolkit.getScreenSize()
        def dialogsize = dialog.size
        def framesize
        def location

        if (frame != null && frame.visible) {
            location = frame.getLocation()
            framesize = frame.getSize()
        }
        else {
            location = new Point(0, 0)
            framesize = screensize
        }

        boolean changed = false
        if (dialogsize.width > (screensize.width - 40)) {
            dialogsize.width = screensize.width - 40
            changed = true
        }
        if (dialogsize.height > (screensize.height - 40)) {
            dialogsize.height = screensize.height - 40
            changed = true
        }
        if (changed) {
            dialog.size = dialogsize
        }

        def x = ((framesize.width - dialogsize.width) / 2) + location.@x
        def y = ((framesize.height - dialogsize.height) / 2) + location.@y

        if (x < 0) {
            x = 0
        }
        else {
            if ((x + dialogsize.width) > screensize.width) {
                x = screensize.width - dialogsize.width
            }
        }

        if (y < 0) {
            y = 0
        }
        else {
            if ((y + dialogsize.height) > screensize.height) {
                y = screensize.height - dialogsize.height
            }
        }

        dialog.setLocation(x as Integer, y as Integer)
    }
	
	static Color getLineBorderColor() {
		
		UIManager.getColor('Button.darkShadow')
	}
}

